{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# OCO Orders"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This section is about **OCO (\"One Cancels Other\") orders**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import fxcmpy\n",
    "import pandas as pd\n",
    "import datetime as dt\n",
    "con = fxcmpy.fxcmpy(config_file='fxcm.cfg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating an OCO Order"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To begin with, check **the open positons**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_orders()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "OCO orders are created by the method `con.create_oco_order()`. A detailed description of the method's parameters is found in section _API Documentation_.    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "oco_order = con.create_oco_order(account_id='2815291', \n",
    "                                 symbol='EUR/USD', \n",
    "                                 is_buy=True, is_buy2=False, \n",
    "                                 amount=30, is_in_pips= False, \n",
    "                                 time_in_force='GTC',\n",
    "                                 at_market=1, \n",
    "                                 order_type='MarketRange', \n",
    "                                 expiration='01122017',\n",
    "                                 limit=1.13, limit2=1.12, \n",
    "                                 rate=1.11, rate2=1.13,\n",
    "                                 stop=1.1, stop2=1.15, \n",
    "                                 trailing_step=0, trailing_step2=0, \n",
    "                                 trailing_stop_step=0, \n",
    "                                 trailing_stop_step2=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `con.oco_orders` attribute is a `dict` object, containing all existing OCO orders as values and their `ids` as keys.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{96414821: <fxcm.fxcm_oco_order at 0x7f3535cccdd8>}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.oco_orders                    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`con.get_oco_order_ids()` returns the OCO order `ids`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[96414821]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_oco_order_ids()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "After the OCO order placement, two orders a active. The `ocoBulkId` value connects the two orders to the OCO order."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>accountId</th>\n",
       "      <td>2815291</td>\n",
       "      <td>2815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>accountName</th>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>amountK</th>\n",
       "      <td>30</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buy</th>\n",
       "      <td>1.11</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currency</th>\n",
       "      <td>EUR/USD</td>\n",
       "      <td>EUR/USD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currencyPoint</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>expireDate</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isBuy</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isELSOrder</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isEntryOrder</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isLimitOrder</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isNetQuantity</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isStopOrder</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limit</th>\n",
       "      <td>1.13</td>\n",
       "      <td>1.12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limitPegBaseType</th>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limitRate</th>\n",
       "      <td>1.13</td>\n",
       "      <td>1.12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ocoBulkId</th>\n",
       "      <td>96414821</td>\n",
       "      <td>96414821</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>orderId</th>\n",
       "      <td>96414820</td>\n",
       "      <td>96414825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>range</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ratePrecision</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sell</th>\n",
       "      <td>0</td>\n",
       "      <td>1.13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>status</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stop</th>\n",
       "      <td>1.1</td>\n",
       "      <td>1.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopMove</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopPegBaseType</th>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopRate</th>\n",
       "      <td>1.1</td>\n",
       "      <td>1.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <td>01122018120146842</td>\n",
       "      <td>01122018120146842</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>timeInForce</th>\n",
       "      <td>GTC</td>\n",
       "      <td>GTC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tradeId</th>\n",
       "      <td>96414822</td>\n",
       "      <td>96414826</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>type</th>\n",
       "      <td>LE</td>\n",
       "      <td>SE</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  0                  1\n",
       "accountId                   2815291            2815291\n",
       "accountName                02815291           02815291\n",
       "amountK                          30                 30\n",
       "buy                            1.11                  0\n",
       "currency                    EUR/USD            EUR/USD\n",
       "currencyPoint                     3                  3\n",
       "expireDate                                            \n",
       "isBuy                          True              False\n",
       "isELSOrder                    False              False\n",
       "isEntryOrder                   True               True\n",
       "isLimitOrder                   True              False\n",
       "isNetQuantity                 False              False\n",
       "isStopOrder                   False               True\n",
       "limit                          1.13               1.12\n",
       "limitPegBaseType                 -1                 -1\n",
       "limitRate                      1.13               1.12\n",
       "ocoBulkId                  96414821           96414821\n",
       "orderId                    96414820           96414825\n",
       "range                             0                  0\n",
       "ratePrecision                     5                  5\n",
       "sell                              0               1.13\n",
       "status                            1                  1\n",
       "stop                            1.1               1.15\n",
       "stopMove                          0                  0\n",
       "stopPegBaseType                  -1                 -1\n",
       "stopRate                        1.1               1.15\n",
       "t                                 3                  3\n",
       "time              01122018120146842  01122018120146842\n",
       "timeInForce                     GTC                GTC\n",
       "tradeId                    96414822           96414826\n",
       "type                             LE                 SE"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_orders().T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Changing OCO Orders"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To add an existing order to an OCO order, the method `con.add_to_oco()` is used.\n",
    "\n",
    "First, an entry **order is created**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "order = con.create_entry_order(symbol='USD/JPY', is_buy=True, rate = 110,\n",
    "                               amount=50, is_in_pips = False, \n",
    "                               time_in_force='GTC', stop=None,\n",
    "                               limit=112, trailing_step=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The new order's `id` is:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "order_id = order.get_orderId()\n",
    "order_id"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The order's `ocoBulkId` should be 0:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order.get_ocoBulkId()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Second, the **order is added** to the OCO order via the `ocoBulkId` value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "bulk_id = con.get_oco_order_ids()[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "con.add_to_oco(oco_bulk_id=bulk_id, order_ids=[order_id])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the `ocoBulkId` value is the same as for the OCO order from above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "96414821"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order.get_ocoBulkId()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Removing an order** is accomplished via the `con.remove_from_oco()` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "con.remove_from_oco(order_ids=[order_id])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order.get_ocoBulkId()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once can also **add and remove orders** in one step, just use `edit_oco_order()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "order_id_2 = con.get_order_ids()[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "con.edit_oco(oco_bulk_id=bulk_id, \n",
    "             remove_order_ids=[order_id_2],\n",
    "             add_order_ids=[order_id])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>accountId</th>\n",
       "      <td>2815291</td>\n",
       "      <td>2815291</td>\n",
       "      <td>2815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>accountName</th>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>amountK</th>\n",
       "      <td>30</td>\n",
       "      <td>30</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buy</th>\n",
       "      <td>1.11</td>\n",
       "      <td>0</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currency</th>\n",
       "      <td>EUR/USD</td>\n",
       "      <td>EUR/USD</td>\n",
       "      <td>USD/JPY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currencyPoint</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4.49865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>expireDate</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isBuy</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isELSOrder</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isEntryOrder</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isLimitOrder</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isNetQuantity</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isStopOrder</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limit</th>\n",
       "      <td>1.13</td>\n",
       "      <td>1.12</td>\n",
       "      <td>112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limitPegBaseType</th>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limitRate</th>\n",
       "      <td>1.13</td>\n",
       "      <td>1.12</td>\n",
       "      <td>112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ocoBulkId</th>\n",
       "      <td>0</td>\n",
       "      <td>96414821</td>\n",
       "      <td>96414821</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>orderId</th>\n",
       "      <td>96414820</td>\n",
       "      <td>96414825</td>\n",
       "      <td>96414855</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>range</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ratePrecision</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sell</th>\n",
       "      <td>0</td>\n",
       "      <td>1.13</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>status</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stop</th>\n",
       "      <td>1.1</td>\n",
       "      <td>1.15</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopMove</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopPegBaseType</th>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopRate</th>\n",
       "      <td>1.1</td>\n",
       "      <td>1.15</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <td>01122018120219537</td>\n",
       "      <td>01122018120146842</td>\n",
       "      <td>01122018120219537</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>timeInForce</th>\n",
       "      <td>GTC</td>\n",
       "      <td>GTC</td>\n",
       "      <td>GTC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tradeId</th>\n",
       "      <td>96414822</td>\n",
       "      <td>96414826</td>\n",
       "      <td>96414856</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>type</th>\n",
       "      <td>LE</td>\n",
       "      <td>SE</td>\n",
       "      <td>LE</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  0                  1                  2\n",
       "accountId                   2815291            2815291            2815291\n",
       "accountName                02815291           02815291           02815291\n",
       "amountK                          30                 30                 50\n",
       "buy                            1.11                  0                110\n",
       "currency                    EUR/USD            EUR/USD            USD/JPY\n",
       "currencyPoint                     3                  3            4.49865\n",
       "expireDate                                                               \n",
       "isBuy                          True              False               True\n",
       "isELSOrder                    False              False              False\n",
       "isEntryOrder                   True               True               True\n",
       "isLimitOrder                   True              False               True\n",
       "isNetQuantity                 False              False              False\n",
       "isStopOrder                   False               True              False\n",
       "limit                          1.13               1.12                112\n",
       "limitPegBaseType                 -1                 -1                 -1\n",
       "limitRate                      1.13               1.12                112\n",
       "ocoBulkId                         0           96414821           96414821\n",
       "orderId                    96414820           96414825           96414855\n",
       "range                             0                  0                  0\n",
       "ratePrecision                     5                  5                  3\n",
       "sell                              0               1.13                  0\n",
       "status                            1                  1                  1\n",
       "stop                            1.1               1.15                  0\n",
       "stopMove                          0                  0                  0\n",
       "stopPegBaseType                  -1                 -1                 -1\n",
       "stopRate                        1.1               1.15                  0\n",
       "t                                 3                  3                  3\n",
       "time              01122018120219537  01122018120146842  01122018120219537\n",
       "timeInForce                     GTC                GTC                GTC\n",
       "tradeId                    96414822           96414826           96414856\n",
       "type                             LE                 SE                 LE"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_orders().T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The `fxcm_oco_order` Class"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The methods `con.get_oco_order()` and `con.create_oco_order()` both return an instance of the `fxcm_oco_order` class. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "oco_order = con.get_oco_order(bulk_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<fxcm.fxcm_oco_order at 0x7f3535cccdd8>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oco_order"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This class provides methods to get the instance's `ocoBulkId` ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "96414821"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oco_order.get_ocoBulkId()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "... the `ids` of the contained orders ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[96414825, 96414855]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oco_order.get_order_ids()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "... and the order objects itself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<fxcm.fxcm_order at 0x7f3535646cf8>, <fxcm.fxcm_order at 0x7f3535668cc0>]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oco_order.get_orders()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The class has also methods to **add or remove existing orders**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_order = con.create_entry_order(symbol='GBP/USD', is_buy=True,\n",
    "                                   amount=300, rate= 1.36,\n",
    "                                   limit=1.37, is_in_pips = False,\n",
    "                                   time_in_force='GTC')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "oco_order.add_order([new_order])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<fxcm.fxcm_order at 0x7f3535646cf8>,\n",
       " <fxcm.fxcm_order at 0x7f3535668cc0>,\n",
       " <fxcm.fxcm_order at 0x7f3534e00048>]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oco_order.get_orders()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "oco_order.remove_order([new_order])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is also possible to add orders to or remove from the OCO order **in a single step**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "order = oco_order.get_orders()[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "oco_order.edit_order(add_orders=[new_order], remove_orders=[order])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[96414855, 96414934]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oco_order.get_order_ids()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
